<?php
namespace ElementVip\JwOms\Http\Controllers;

use ElementVip\Backend\Http\Controllers\Controller;
use ElementVip\JwOms\Handler\GoodsHandle;
use ElementVip\JwOms\Models\OmsDownloadLog;
use ElementVip\JwOms\Models\OmsSyncLog;
use ElementVip\JwOms\Models\OmsUploadLog;
use ElementVip\JwOms\Services\GoodsService;
use ElementVip\JwOms\Services\OrderService;
use ElementVip\JwOms\Services\RefundService;
use Illuminate\Http\Request;
use Storage;
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/7/5
 * Time: 13:30
 */
class OmsFileController extends Controller
{

    /**
     * 上传文件列表
     * @return mixed
     */
    public function uploadFiles()
    {
        $status = request('status') ? request('status') : 1;

        $files = OmsUploadLog::where('status', $status)->where('type',request('type'))->orderBy('id', 'desc')->paginate(15);

        return view('oms-backend::files.upload', compact('files'));
    }

    /**
     * 删除上传的文件
     * @param $id
     * @return mixed
     */
    public function deleteUploadFile($id)
    {
        $file = OmsUploadLog::find($id);
        if ($file) {
            $file->delete();
            return $this->ajaxJson();
        }
        return $this->ajaxJson(false);
    }

    /**
     * 下载文件列表
     * @return mixed
     */
    public function downFiles()
    {
        $status = request('status') ? request('status') : 1;

        $files = OmsDownloadLog::where('status', $status)->where('type',request('type'))->orderBy('id', 'desc')->paginate(15);

        return view('oms-backend::files.download', compact('files'));
    }

    /**
     * 删除下载的文件
     * @param $id
     * @return mixed
     */
    public function deleteDownFile($id)
    {
        $file = OmsDownloadLog::find($id);
        if ($file) {
            $file->delete();
            return $this->ajaxJson();
        }
        return $this->ajaxJson(false);
    }

    /**
     * 数据同步日志
     * @return mixed
     */
    public function syncLogs()
    {
        $logs = OmsSyncLog::where('type', request('type'))->orderBy('id', 'desc')->paginate(15);
        return view('oms-backend::logs.index', compact('logs'));
    }

    public function showLogs($id)
    {
        $log = OmsSyncLog::find($id);
        return view('oms-backend::logs.show', compact('log'));
    }

    /**
     * 手动同步全库存
     * @param GoodsHandle $goodsHandle
     */
    public function syncStock(GoodsHandle $goodsHandle)
    {
        $goodsHandle->handle();        
    }

    /**
     * 手工同步数据
     * @return mixed
     */
    public function syncModal()
    {
        $file = request('file');
        $type = request('type');
        return view('oms-backend::files.sync_modal', compact('file', 'type'));
    }

    public function syncData(GoodsService $goodsService, OrderService $orderService, RefundService $refundService)
    {
        $file = storage_path('app/public/' . strstr(request('file'), 'oms'));

        $type = request('type');
        switch ($type) {
            case 'Stock':
                $goodsService->syncStock($file, $type);
                break;
            case 'InvupStock':
                $goodsService->syncStock($file, $type);
                break;
            case 'OrderSendShip':
                $orderService->orderShip($file);
                break;
            case 'OrderReturnShip':
                $refundService->syncRefund($file);
                break;

        }

        return $this->ajaxJson();
    }
    
    /**
     * FTP设置
     */
    public function ftpSetting()
    {       
        return view('oms-backend::setting');
    }

    public function saveSettings(Request $request)
    {
        $data = $request->except('_token');

        settings()->setSetting($data);

        $this->ajaxJson();
    }
}